﻿@{
    ViewData["Title"] = "UpdataView";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link href="~/layui/lib/layui-v2.6.3/css/layui.css" rel="stylesheet" />
    <link href="~/layui/css/public.css" rel="stylesheet" />
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>


    <div class="layui-form layuimini-form" lay-filter="formTest">
        <div class="layui-form-item">
            <label class="layui-form-label required">部门名称</label>
            <div class="layui-input-block">
                <input type="text" name="DepartmentName" lay-verify="required" lay-reqtext="部门名称不能为空" placeholder="请输入部门名称" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">主管</label>
            <div class="layui-input-inline">
                <select id="service" name="LeaderId" lay-verify="required" class="select">
                    <option value="0">请选部门</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">父级部门</label>
            <div class="layui-input-inline">
                <input type="text" name="ParentId" id="parentTree" lay-filter="parentTree" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-required">描述：</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入描述" oninput="wordLeg(this);" maxlength="100" onpropertychange="if(value.length>100) value=value.substr(0,100)" name="Description" class="layui-textarea"></textarea>
                <div class="word"><span class="text_count">0</span>&nbsp;/&nbsp;<span class="num_count" id="numCount">100</span></div>
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <input type="hidden" name="Id">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <input type="hidden" name="CreateTime">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>


    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/layui/lib/layui-v2.6.3/layui.js"></script>
    <script>

        //树目录
        layui.config({
            base: ""
        }).extend({
            treeSelect: "../layui/js/lay-module/treeSelect/treeSelect"
        });

        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            //接收主页传过来的数据
            var type = GetQueryString("date");
            //封装GetQueryString()方法来获取URL后面的value值
            function GetQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null)
                    return decodeURI(r[2]);
                return null;
            }
            //渲染部门树目录
            var parentId = "";
            layui.use(["treeSelect"], function () {
                var treeSelect = layui.treeSelect;
                treeSelect.render({
                    elem: '#parentTree',
                    data: "/Department/getDepartmentSelect",
                    type: 'get',
                    placeholder: '请选择父部门',
                    search: true,
                    style: {
                        folder: {
                            enable: false // 圈圈图标
                        },
                        line: {
                            enable: true  // 线
                        }
                    },
                    click: function (d) {
                        console.log(d); // 点击回调
                    },
                    success: function (d) {
                        console.log(parentId)
                        treeSelect.checkNode('parentTree', parentId); //选中节点，根据id筛选
                        treeSelect.refresh('parentTree'); //刷新树结构
                    }
                });

            })

            //描述字数
            window.wordLeg = function (obj) {
                var currleg = $(obj).val().length;
                var length = $(obj).attr('maxlength');
                if (currleg > length) {
                    layer.msg('字数请在' + length + '字以内');
                } else {
                    $('.text_count').text(currleg);
                }
            }
            //渲染列表
            $.ajax({
                url: "/Department/getByDepartment",
                type: "get",
                data: { departmentid: type },
                success: function (res) {
                    //主管下拉框
                    if (res.data.leader != null) {
                        $("#service").empty();
                        $("#service").append(new Option("请选择部门", "0"));
                        $.each(res.data.leader, function (index, value) {
                            $('#service').append(new Option(value.userName, value.id));// 下拉菜单里添加元素
                            //选中状态
                            if (value.id == res.data.department.leaderId) {
                                $('#service').children('option[value="' + value.id + '"]').attr('selected', value.userName);
                            }
                         
                        });
                    } else {
                        $("#service").append(new Option("暂无数据", ""));
                    }
                    //重新渲染下拉框
                    form.render("select");

                    console.log(res)
                    //给表单赋值
                    form.val("formTest", {
                        "Id": res.data.department.id,
                        "DepartmentName": res.data.department.departmentName
                        , "CreateTime": res.data.department.createTime
                        , "Description": res.data.department.description
                    });
                    //树目录选中状态
                    parentId = res.data.department.parentId;
                }
            }),
                //监听提交（修改）
                form.on('submit(saveBtn)', function (data) {
                    $.ajax({
                        url: "/Department/UpdataDepartment",
                        type: "put",
                        data: data.field,
                        success: function (res) {
                            if (res.data) {
                                layer.alert(res.msg, { icon: 1 }, function () {
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                })
                            } else {
                                layer.msg(res.msg, { icon: 5 });
                            }
                        }
                    }),
                        // 关闭弹出层
                        layer.close(index);
                });

        });</script>
</body>
</html>


